package org.aokey.springdemo3.July;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpEchoServer1 {
    DatagramSocket socket=null;
    public UdpEchoServer1(int port) throws SocketException {
        socket=new DatagramSocket(port);
    }
    public void Start() throws IOException {
        System.out.println("服务器上线");
        while(true){
            DatagramPacket packet=new DatagramPacket(new byte[1024],1024);
            socket.receive(packet);
            String request=new String(packet.getData(),0,packet.getLength());
            String response=process(request);
            DatagramPacket responsePacket=new DatagramPacket(response.getBytes(),response.getBytes().length,packet.getAddress(),packet.getPort());
            socket.send(responsePacket);
            System.out.printf("[%s:%d]    req:%s    rep:%s",packet.getAddress(),packet.getPort(),request,response);
        }
    }
    public String process(String request){
        return  request;
    }
    public static void main(String[] args) throws IOException {
        UdpEchoServer1 server=new UdpEchoServer1(8080);
        server.Start();
    }
}
